import { useUserStore } from '@/stores'
import { createRouter, createWebHistory } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
NProgress.configure({
  showSpinner: false
})
const router = createRouter({
  // createWebHistory()中的输入参数，为vuerouter的基础路径
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/Login/index.vue'),
      meta: { title: '登录' }
    },
    {
      path: '/',
      redirect: '/home',
      component: () => import('@/views/Layout/index.vue'),
      children: [
        {
          path: '/home',
          component: () => import('@/views/Home/index.vue'),
          meta: { title: '首页' }
        },
        {
          path: '/contact',
          component: () => import('@/views/Contact/index.vue'),
          meta: { title: '通讯录' }
        },
        {
          path: '/find',
          component: () => import('@/views/Find/index.vue'),
          meta: { title: '发现' }
        },
        {
          path: '/my',
          component: () => import('@/views/My/index.vue'),
          meta: { title: '个人中心' }
        }
      ]
    },
    {
      path: '/moments',
      component: () => import('@/views/Monents/index.vue')
    },
    {
      path: '/momentsDetail/:id',
      component: () => import('@/views/Monents/detail/index.vue')
    },
    {
      path: '/chat',
      component: () => import('@/views/Chat/index.vue')
    },
    {
      path: '/startChat',
      component: () => import('@/views/StartChat/index.vue')
    },
    {
      path: '/GroupChat',
      component: () => import('@/views/GroupChat/index.vue')
    },
    {
      path: '/addGroup',
      component: () => import('@/views/addGroup/index.vue')
    },
    {
      path: '/addFriend',
      component: () => import('@/views/addFriend/index.vue')
    },
    {
      path: '/userInfo/:id',
      component: () => import('@/views/User/index.vue')
    },
    {
      path: '/groupInfo/:id',
      component: () => import('@/views/GroupUser/index.vue')
    },
    {
      path: '/newFriend',
      component: () => import('@/views/NewFriend/index.vue')
    },
    {
      path: '/addMonents',
      component: () => import('@/views/addMonent/index.vue')
    },
    {
      path: '/setting',
      component: () => import('@/views/setting/index.vue')
    },
    {
      path: '/changePassword',
      component: () => import('@/views/setting/ChangePwd.vue')
    }
  ]
})
router.beforeEach((to) => {
  //获取token
  const store = useUserStore()
  const whiteList = ['/login']
  // if (!store.user?.token && !whiteList.includes(to.path)) return '/login'
  // 如果用户未登录且目标路由不在白名单中，跳转到登录页
  if (!store.user?.token && !whiteList.includes(to.path)) {
    NProgress.start() // 启动进度条
    return '/login'
  }

  // 如果用户已登录且目标路由在白名单中，跳转到首页
  if (store.user?.token && whiteList.includes(to.path)) {
    NProgress.start() // 启动进度条
    return '/home'
  }

  // 其他情况允许导航
  NProgress.start() // 启动进度条
  return true
})
router.afterEach((to) => {
  NProgress.done()
  document.title = `${to.meta.title || ''}-在线聊天`
})
export default router
